package com.sunlg.smarthome.ui.guard;

import android.support.design.widget.TextInputEditText;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.AddSecurityDeviceContract;
import com.sunlg.smarthome.contract.presenter.AddSecurityDevicePresenter;
import com.sunlg.smarthome.data.entity.SecurityDevice;

import butterknife.BindView;

public class AddSecurityDeviceActivity extends BaseActivity implements AddSecurityDeviceContract.View{
    @BindView(R.id.et_1)
    TextInputEditText etNo;
    @BindView(R.id.et_2)
    TextInputEditText etDesc;
    @BindView(R.id.spinner_type)
    Spinner spinnerType;

    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private int type;

    AddSecurityDeviceContract.Presenter presenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_add_security_device;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("添加设备");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        presenter = new AddSecurityDevicePresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView() {
        spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                type = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                type = 0;
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String noStr = etNo.getText().toString();
                String desc = etDesc.getText().toString();
                if(TextUtils.isEmpty(noStr)){
                    toast("序号不能为空");
                }else {
                    int no = Integer.valueOf(noStr);
                    SecurityDevice device = new SecurityDevice();
                    device.setType(type);
                    device.setDescription(desc);
                    device.setDevice(no);
                    presenter.addSecurityDevice(device);
                }
            }
        });
    }

    @Override
    public void addSuccess() {
        doFinish();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
    }
}
